// 引入接口文件
import { submitZituoSetting,queryInfo,getProjectList,verifyCodeFun} from './otherConfigAddServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const defultState = {
  modelObj: {
    btnLoading: false,
  },
  loading: false,
  actionType: 'add',
  areaList: [],
  products: [],
  query: {
    id: '',
    areaId:'',
    projectId: '',
    channel: '行销自拓',
    phoneAuthority: '1',
    openBee: '1',
    auditKeyPerson: '',
    openGps: '1',
    viewDetails: '1',
    ztBbbhq: '30',
    ztBbbhqUnit: '1',//1 天 2 小时
    callbackShortTime: '',
    callbackLongTime: '',
    type: '0', //0 一天一班/1 一天两班
    amStartTime: '',
    amEndTime: '',
    pmStartTime: '',
    pmEndTime: '',
    amBeePay: '',
    pmBeePay: '',
    ifAllowCrossProject: '0', //是否报备其他项目
    isAllowsSpecify: '1'//是否指定置业顾问
  },
}

// 声明module
const tmpModule = {
  namespace: 'defaultModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: { ...cloneDeep(defultState), ...query } })//每次进去页面先重置状态
          // 执行业务操作
          dispatch({type: 'verifyCodeFun',payload: {query}})
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    *verifyCodeFun({payload},{put,select,call}){
      if(yield call(verifyCodeFun,{})){
        if(payload.query.actionType == "detail"){
          yield put({
            type: 'queryInfo',
            payload: payload
          }) // end dispatch
        }
        yield put({type: 'getProjectList', payload: {}})
      }
    },
    *queryInfo({payload}, {put, select, call}){
      let {query} = yield select(state => state[tmpModule.namespace])
      let params = { "id":payload.query.id }
      const { success, msg, zituoSetting } = yield call(queryInfo, {...params})
      if(success){
        query = zituoSetting
        yield put({
          type: 'updateStore',
          payload: {query}
        })
        yield put({type: 'getProjectList', payload: {areaId:query.areaId}})
      }
    },
      //获取区域项目
      *getProjectList({payload},{put,select,call}){
      let {areaList} = yield select(state => state[tmpModule.namespace])
      let params = {
          level : 2
        }
        const { data } = yield call(getProjectList, {...params})
          let list = data
          if(!list.length){
            return
          }
          areaList =  list.map((item,index)=>{
            return {
              label: item.buname,
              value: item.buguid,
              children: item.childSysUninessunitList
            }
          })
          yield put({type: 'updateStore', payload: {areaList}})
          if(payload.areaId){
            yield put({type: 'filterProjectList', payload: {areaId:payload.areaId}})
        }
    },
    *submit({payload,callback},{put,select,call}){
      let {query} = yield select(state => state[tmpModule.namespace])
      let datas = {
          id:query.id,
          areaId:query.areaId,
          projectId: query.projectId,
          channel: query.channel,
          openBee: query.openBee,
          auditKeyPerson: '0',
          openGps: query.openGps,
          viewDetails: query.viewDetails,
          ztBbbhq: query.ztBbbhq,
          ztBbbhqUnit: query.ztBbbhqUnit,//1 天 2 小时
          callbackShortTime: query.callbackShortTime,
          callbackLongTime: query.callbackLongTime,
          type: query.type, //0 一天一班/1 一天两班
          amStartTime: query.amStartTime?query.amStartTime: '',
          amEndTime: query.amEndTime?query.amEndTime:'',
          pmStartTime: query.pmStartTime?query.pmStartTime:'',
          pmEndTime: query.pmEndTime?query.pmEndTime:'',
          amBeePay: query.amBeePay?query.amBeePay:'',
          pmBeePay: query.pmBeePay?query.pmBeePay:'',
          phoneAuthority: query.phoneAuthority?query.phoneAuthority:'',
          ifAllowCrossProject: query.ifAllowCrossProject?query.ifAllowCrossProject:'0',
          isAllowsSpecify: query.isAllowsSpecify?query.isAllowsSpecify:'1',
      }
      let params = datas
      const { success, msg, data } = yield call(submitZituoSetting, {...params})
      if(success){
        message.success('操作成功');
        callback(); // 返回结果
      }
    },
    *filterProjectList({payload},{put,select,call}){    
      let {products,areaList,selectData} = yield select(state => state[tmpModule.namespace])
      let list = []
      for(var item of areaList){
        if(item.value == payload.areaId){
          list = item.children
        }
      }
      if(list.length){
        products =  list.map((item,index)=>{
          return {
            label: item.buname,
            value: item.buguid,
            userId: '',
            selectOptionData: []
          }
        })
      }
      yield put({type: 'updateStore', payload: {products}})
    },
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
